package timbap.visualizer.api.plugin.container;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import timbap.visualizer.api.plugin.Plugin;

/**
 * The <code>AbstractPluginControllableSwitch</code> provides a
 * common base implementation of the <code>PluginControllableSwitch</code>
 * interface that should be used by all plugin switches that can
 * be controlled by their plugins.
 *
 * @author  Andreas Pabst
 * @see     timbap.visualizer.api.plugin.container.PluginControllableSwitch
 * @see     timbap.visualizer.api.plugin.container.AbstractPluginSwitch
 * @see     timbap.visualizer.api.plugin.container.PluginSwitchCommandPublisher
 */
public abstract class AbstractPluginControllableSwitch extends AbstractPluginSwitch implements PluginControllableSwitch {

	private static final Log LOG = LogFactory.getLog(AbstractPluginControllableSwitch.class);

    @Override
    public void addPlugin(Plugin pPlugin) {
        super.addPlugin(pPlugin);
        if (pPlugin instanceof PluginSwitchCommandPublisher) {
            PluginSwitchCommandPublisher tSwitchPublisher = (PluginSwitchCommandPublisher)pPlugin;
            tSwitchPublisher.addPluginSwitchCommandListener(this);
        }
    }

    @Override
    public void removePlugin(Plugin pPlugin) {
        if (pPlugin instanceof PluginSwitchCommandPublisher) {
            PluginSwitchCommandPublisher tSwitchPublisher = (PluginSwitchCommandPublisher)pPlugin;
            tSwitchPublisher.removePluginSwitchCommandListener(this);
        }
    }

    public void handlePluginSwitchCommand(PluginSwitchCommand pPluginSwitchCommand) {

    	if (pPluginSwitchCommand == PluginSwitchCommand.NEXT) {
    		toNext();
        } else if (pPluginSwitchCommand == PluginSwitchCommand.PREVIOUS) {
        	toPrevious();
        } else if (pPluginSwitchCommand == PluginSwitchCommand.ERROR) {
        	toError();
        }
    }


}